package examen.miguel.remirez.struts2;



import java.util.List;

import org.hibernate.Session;
import org.hibernate.Transaction;

import com.googlecode.s2hibernate.struts2.plugin.annotations.SessionTarget;
import com.googlecode.s2hibernate.struts2.plugin.annotations.TransactionTarget;

public class DivisaDAO {

	@SessionTarget
	Session session;

	@TransactionTarget
	Transaction transaction;

	public void guadgarOActualizarDivisa(Divisa divisa) {
		try {
			session.saveOrUpdate(divisa);
		} catch (Exception e) {
			transaction.rollback();
			e.printStackTrace();
		}
	}
	
	
	public void eliminarDivisa(Long id) {
		try {
			Divisa divisa = (Divisa) session.get(Divisa.class, id);
			session.delete(divisa);
		} catch (Exception e) {
			transaction.rollback();
			e.printStackTrace();
		} 
	}
	
	
	@SuppressWarnings("unchecked")
	public List<Divisa> listarDivisas(){
		List<Divisa> divisas = null;
		try {
			divisas = session.createQuery("from divisa").list();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return divisas;
	}

	
	public Divisa listarDivisaPorId(Long id) {
		Divisa divisa = null;
		try {
			divisa = (Divisa) session.get(Divisa.class, id);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return divisa;
	}

}